package com.qianya.system.manage.web.sys.controller;

import com.qianya.model.common.CommonResult;
import com.qianya.system.manage.web.sys.model.ServerInfo;
import com.qianya.util.BaseCommon;
import com.sun.management.OperatingSystemMXBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.mvel2.util.Make;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.lang.management.ManagementFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.qianya.util.RandUtils.*;

@Slf4j
@RestController
@RequestMapping("/sys/info")
@Api(value = "GetInfoController", tags = "系统设置-服务器信息")
public class GetInfoController {




    @SuppressWarnings("restriction")
    @ApiOperation("获取服务器信息")
    @GetMapping(value = "/getMemInfo")
    public CommonResult getMemInfo() {

        Sigar sigar = new Sigar();
        CpuPerc perc = null;
        CpuInfo[] cpuInfoList = new CpuInfo[0];

        try {
            cpuInfoList = sigar.getCpuInfoList();
            perc = sigar.getCpuPerc();
        } catch (SigarException e) {
            e.printStackTrace();
        }


        ServerInfo serverInfo = new ServerInfo();

        serverInfo.setSystemName(System.getProperty("os.name"));

        serverInfo.setGetCpuCount(cpuInfoList.length);

        serverInfo.setIpAddress(BaseCommon.getIp());

        serverInfo.setGetCpuUsage(perc.getCombined());

        HashMap[] data = new HashMap[1];

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("操作系统",serverInfo.getSystemName());
        objectObjectHashMap.put("CPU核心数",serverInfo.getGetCpuCount());
        objectObjectHashMap.put("ip地址",serverInfo.getIpAddress());
        objectObjectHashMap.put("CPU使用率",serverInfo.getGetCpuUsage());

        data[0]=objectObjectHashMap;

        return CommonResult.success(data);
    }

}


